<?php

class admin_app_theme extends admin_app
{

	public function __construct()
	{
		$my = bone::get_admin();
		if($my->guest)$this->redirect('./?app=user&task=login', '登陆超时', 'error');
		
		parent::__contruct( 'theme', '主题管理' );
	}


	public function listing()
	{
		$model = $this->get_model('template');
		$theme_list = $model->get_theme_list();

		$template = $this->get_template('listing');
		$template->set_title('主题列表');
		$template->set('theme_list', $theme_list);
		$template->display();
	}

	public function ajax_make_default()
	{
		$theme_id = get('theme_id', 0, 'int');
		if($theme_id==0)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{theme_id}缺失');
			$this->ajax();
		}
		$model = $this->get_model('template');
		$model->make_default($theme_id);

		$this->set_ajax('status', 0);
		$this->set_ajax('description', '设置成功');
		$this->ajax();
	}

	public function ajax_delete()
	{
		$theme_id = get('theme_id', 0, 'int');
		if($theme_id==0)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{theme_id}缺失');
			$this->ajax();
		}
		$model = $this->get_model('template');
		$model->delete($theme_id);

		$this->set_ajax('status', 0);
		$this->set_ajax('description', '删除成功');
		$this->ajax();
	}


	public function online_listing()
	{
		$key = get('key');
		$template = $this->get_template('online_listing');

		$config = $this->get_config();
		$soap = new SoapClient(null, array('location'=>$config->uri_theme, 'uri'=>$config->uri));

		$limit = 8;
		
		$pagination = $template->get_pagination();
		$pagination->set_limit( $limit );
		$pagination->set_page( get('page', 1, 'int') );
		if($key)
		{
			$pagination->set_total( $soap->get_search_theme_total($key) );
			$pagination->set_url('./?app=template&task=online_listing&key='.$key);
			$theme_list = $soap->get_search_theme_list($key, $pagination->get_offset(), $limit);
		}
		else
		{
			$pagination->set_total( $soap->get_theme_total() );
			$pagination->set_url('./?app=template&task=online_listing');
			$theme_list = $soap->get_theme_list($pagination->get_offset(), $limit);
		}

		$model = $this->get_model('template');
		$installed_theme_list = $model->get_theme_list();
		$installed_theme = array();
		foreach($installed_theme_list as $theme)
		{
			$installed_theme[] = $theme->theme_id;
		}

		$template->set_title('安装新主题');
		$template->set('theme_list', $theme_list);
		$template->set('installed_theme', $installed_theme);
		$template->display();
	}


	public function ajax_install()
	{
		$theme_id = get('theme_id', 0, 'int');
		if($theme_id==0)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{theme_id}缺失');
			$this->ajax();
		}

		$config = $this->get_config();
		$soap = new SoapClient(null, array('location'=>$config->uri_theme, 'uri'=>$config->uri));
		$theme = $soap->get_theme($theme_id);
		
		if(!$theme)
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', '没有找到指定的主题');
			$this->ajax();
		}

		$tmp_path = DIR.'tmp'.DS.'themes'.DS.$theme->theme.'.zip';
		copy('http://www.mrbone.org/?app=client_theme&task=download&theme_id='.$theme_id, $tmp_path);

		$theme_folder = BONE_ROOT.DS.'apps'.DS.'theme'.DS.'templates'.DS.$theme->theme.DS;
		
		$zip = bone::get('zip');
		$zip->open($tmp_path);
		if(!$zip->extract_to($theme_folder))
		{
			$this->set_ajax('status', 3);
			$this->set_ajax('description', $zip->get_error());
			$this->ajax();
		}

		unlink($tmp_path);

		$table = $this->get_table('template');
		$table->bind($theme);
		$table->id = 0;
		$table->theme_id = $theme->id;
		$table->thumbnail = 'http://www.mrbone.org/'.$theme->thumbnail;
		$table->image = 'http://www.mrbone.org/'.$theme->image;
		$table->is_default = 0;
		$table->install_time = time();
		$table->save();

		$this->set_ajax('status', 0);
		$this->set_ajax('description', '安装成功');
		$this->ajax();
	}

	
}
?>